package com.hemerasolutions.v2trainer.component.list;



import java.util.List;

import javax.annotation.PostConstruct;

import org.primefaces.event.FileUploadEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.list.PhotosListCompInterface;
import com.hemerasolutions.v2trainer.model.multimedia.Photo;
import com.hemerasolutions.v2trainer.service.interfaces.EquipmentServiceInterface;
import com.hemerasolutions.v2trainer.service.interfaces.ModalityServiceInterface;
import com.hemerasolutions.v2trainer.service.interfaces.MultimediaServiceInterface;

@Controller
@Component("photosListComp")
@Scope("prototype")
public class PhotosListComp extends BaseComponent implements PhotosListCompInterface {

	private static final long serialVersionUID = 1L;

	private List<Photo> photos;
	
	private Photo selPhoto;
	
	@Autowired
	private MultimediaServiceInterface multimediaService;
	
	@Autowired
	private EquipmentServiceInterface equipmentService;

	@Autowired
	private ModalityServiceInterface modalityService;
	
	private int photoType = 1;
	
	public static final int EXERCISE_PHOTO = 1;

	public static final int EQUIPMENT_PHOTO = 2;
	
	
	
	public PhotosListComp(){
		super();
	}

	@PostConstruct
	public void init(){
		super.init();
	}
	
	
	@Override
	public void addPhoto(){
		Photo photo = null;
		if(photoType == EXERCISE_PHOTO){
			photo = this.multimediaService.readDefaultExercisePhoto();
			this.photos.add(photos.size(),photo);
		}else if(photoType == EQUIPMENT_PHOTO){
			photo = this.multimediaService.readDefaultEquipmentPhoto();
			this.photos.add(photos.size(),photo);
		}
	}
	
	@Override
	public void deletePhoto(){
		this.photos.remove(this.selPhoto);
	}
	
	
	@Override
	public void upload(FileUploadEvent ev) {

		selPhoto.setName(this.photoType == EQUIPMENT_PHOTO ? "Foto de equipamento" : "Foto de Exercício");
		selPhoto.setHeight(240);
		selPhoto.setWidth(320);
		selPhoto = super.uploadPhoto(ev, selPhoto);
		selPhoto.setLocation(multimediaService.persistPhotoInFile(selPhoto));
		
	}	
	
	
	@Override
	public void refresh() {
		Photo photo = null;
		
		if(this.photos!= null && this.photos.isEmpty()){
			if(this.photoType == EXERCISE_PHOTO){
				photo = this.multimediaService.readDefaultExercisePhoto();
				photos.add(photo);
			}else if(this.photoType == EQUIPMENT_PHOTO){
				photo = this.multimediaService.readDefaultEquipmentPhoto();
				photos.add(photo);
			}
		}
	}
		
	

	@Override
	public List<Photo> getPhotos() {
		return photos;
	}

	@Override
	public void setPhotos(List<Photo> photos) {
		this.photos = photos;
	}

	@Override
	public Photo getSelPhoto() {
		return selPhoto;
	}

	@Override
	public void setSelPhoto(Photo selPhoto) {
		this.selPhoto = selPhoto;
	}
	
	@Override
	public int getPhotoType() {
		return photoType;
	}

	@Override
	public void setPhotoType(int photoType) {
		this.photoType = photoType;
	}


	
}
